package net.tv90.bfmccoreqqbot.api.handler;

import net.tv90.bfmccoreqqbot.api.MessageHandler;
import net.tv90.bfmccoreqqbot.api.annotation.MessageCheck;
import net.tv90.bfmccoreqqbot.api.annotation.MessageCheckPolicy;
import net.tv90.bfmccoreqqbot.api.annotation.MessageType;
import net.tv90.bfmccoreqqbot.api.entity.MessageContext;
import net.tv90.bfmccoreqqbot.util.bf4.BF4Util;
import net.tv90.bfmccoreqqbot.util.bf4.UserStats;

@MessageType("bf4")
@MessageCheck(MessageCheckPolicy.STARTS_WITH)
public class BF4StatsMessageHandler implements MessageHandler {
    @Override
    public String handleMessage(MessageContext msg) {

        String[] args = msg.getMessage().split("=");
        if (args.length == 2) {
            String playerName = args[1];
            long start = System.currentTimeMillis();
            UserStats stats = BF4Util.getBF4UserStats(playerName);
            long end = System.currentTimeMillis();
            long time = end - start;
            String footer = "\n本次查询花费" + time + "ms\n-- Powered By BFMCCore-QQBot --\n";
            if (stats != null) {
                return ("{username}的BF4统计信息 来源：Battlelog\n" +
                        "等级：{rank}\n" +
                        "击杀/死亡/助攻：{kills}/{deaths}/{killAssists}\n" +
                        "载具击毁数：{vehicleDestroyed}\n" +
                        "KD/KPM/SPM：{kd}/{kpm}/{spm}\n" +
                        "命中率/爆头率：{acc}/{headshotsRatio}\n" +
                        "最远爆头距离：{longestHeadShots}\n" +
                        "最长连杀数：{longestKillStreak}\n" +
                        "救人数：{revives}\n" +
                        "胜率：{winRate}\n" +
                        "技巧值：{skill}\n" +
                        "分数：{score}\n" +
                        footer)
                        .replace("{username}", playerName)
                        .replace("{kills}", stats.getKills())
                        .replace("{deaths}", stats.getDeaths())
                        .replace("{killAssists}", stats.getKillAssists())
                        .replace("{kd}", stats.getKd())
                        .replace("{winRate}", stats.getWinRate())
                        .replace("{kpm}", stats.getKpm())
                        .replace("{revives}", stats.getRevives())
                        .replace("{rank}", stats.getRank())
                        .replace("{skill}", stats.getSkill())
                        .replace("{score}", stats.getScore())
                        .replace("{acc}", stats.getAcc())
                        .replace("{shotsFired}", stats.getShotsFired())
                        .replace("{shotsHit}", stats.getShotsHit())
                        .replace("{spm}", stats.getSpm())
                        .replace("{vehicleDestroyed}", stats.getVehicleDestroyed())
                        .replace("{headshots}", stats.getHeadshots())
                        .replace("{longestHeadShots}", stats.getLongestHeadShots())
                        .replace("{headshotsRatio}", stats.getHeadshotsRatio())
                        .replace("{longestKillStreak}", stats.getLongestKillStreak())
                        ;
            } else {
                return "无法查询到用户 " + playerName + " 的BF4统计信息" + footer;
            }
        }

        return "格式有误，格式: bf4=玩家名";
    }

    @Override
    public boolean onPreHandleMessage(MessageContext msg) {
        System.out.println("接受到消息: " + msg.getMessage());
        return true;
    }
}
